# This code is hosted on http://code.google.com/p/lenthorp/
# Freely available for use in applications, but should NOT be modified
# Email all comments to lenthorpresearch@gmail.com

import ystockquote
from MySQLSaving import insertData
from datetime import datetime

def getAndSaveData(ticker, dateStart, dateEnd):
    dateStartYahoo = dateStart.strftime('%Y%m%d')
    dateEndYahoo = dateEnd.strftime('%Y%m%d')
    data = ystockquote.get_historical_prices(ticker, dateStartYahoo, dateEndYahoo)
    headers = data[0]
    dat = headers.index('Date')
    close = headers.index('Close')
    dates = []
    rates = []
    for idx in range(1,len(data)):
        dates.append(datetime.strptime(data[idx][dat],'%Y-%m-%d'))
        rates.append(float(data[idx][close]))
    insertData.InsertSQLDateRate(dates, rates, 'historicalDly', ticker)